home *** CD-ROM | disk | FTP | other *** search
/ The PC-SIG Library 10 / The PC-Sig Library - Shareware for the IBM PC and Compatibles (PC-SIG)(Tenth Edition Disks 1-2804)(1991).iso / PC_SIGCD / 22 / 4 / DISK2247.ZIP / CBASE101.ZIP / CBASE.ZIP / CBCLOSE.C < prev    next >
Text File  |  1990-06-21  |  2KB  |  93 lines

  1. /*    Copyright (c) 1989 Citadel    */
  2. /*       All Rights Reserved        */
  3.  
  4. /* #ident    "@(#)cbclose.c    1.4 - 90/06/20" */
  5.  
  6. /* ansi headers */
  7. #include <errno.h>
  8. /*#include <string.h>*/
  9.  
  10. /* library headers */
  11. #include <btree.h>
  12. #include <lseq.h>
  13.  
  14. /* local headers */
  15. #include "cbase_.h"
  16.  
  17. /*man---------------------------------------------------------------------------
  18. NAME
  19.      cbclose - close cbase
  20.  
  21. SYNOPSIS
  22.      #include <cbase.h>
  23.  
  24.      int cbclose(cbp)
  25.      cbase_t *cbp;
  26.  
  27. DESCRIPTION
  28.      The cbclose function causes any buffered data for cbase cbp to be
  29.      written out and the cbase to be unlocked and closed.
  30.  
  31.      cbclose will fail if one or more of the following is true:
  32.  
  33.      [EINVAL]       cbp is not a valid cbase pointer.
  34.      [CBENOPEN]     cbp is not open.
  35.  
  36. SEE ALSO
  37.      cbcreate, cbopen, cbsync.
  38.  
  39. DIAGNOSTICS
  40.      Upon successful completion, a value of 0 is returned.  Otherwise,
  41.      a value of -1 is returned, and errno set to indicate the error.
  42.  
  43. ------------------------------------------------------------------------------*/
  44. int cbclose(cbp)
  45. cbase_t *cbp;
  46. {
  47.     int i = 0;
  48.  
  49.     /* validate input parameters */
  50.     if (!cb_valid(cbp)) {
  51.         errno = EINVAL;
  52.         return -1;
  53.     }
  54.  
  55.     /* check if not open */
  56.     if (!(cbp->flags & CBOPEN)) {
  57.         errno = CBENOPEN;
  58.         return -1;
  59.     }
  60.  
  61.     /* flush buffers and unlock file */
  62.     if (cblock(cbp, CB_UNLCK) == -1) {
  63.         CBEPRINT;
  64.         return -1;
  65.     }
  66.  
  67.     /* close record file */
  68.     if (lsclose(cbp->lsp) == -1) {
  69.         CBEPRINT;
  70.         return -1;
  71.     }
  72.  
  73.     /* close key files */
  74.     for (i = 0; i < cbp->fldc; ++i) {
  75.         if (cbp->fldv[i].flags & CB_FKEY) {
  76.             if (btclose(cbp->btpv[i]) == -1) {
  77.                 CBEPRINT;
  78.                 return -1;
  79.             }
  80.         }
  81.     }
  82.  
  83.     /* free memory allocated for cbase */
  84.     cb_free(cbp);
  85.  
  86.     /* scrub slot in cbb table then free it */
  87.     memset(cbp, 0, sizeof(*cbb));
  88.     cbp->flags = 0;
  89.  
  90.     errno = 0;
  91.     return 0;
  92. }
  93.